<template>
	<form @submit="submit" :data-action="`/api/station/save?x.id=${data.id?data.id:''}&x.companyId=${data.companyId}`">
		
		<block v-for="( item , index) in list" :key="index">
			
			<block v-if="item.type == 'inps'">
				<inps :title="item.title" @input="data[item.name] = $event" :value="data[item.name]"
					:list="item.name == 'factors' ? factorList : item.list" :multi="item.multi" :paddingLeft="item.paddingLeft || 0"></inps>
			</block>
			<block v-else-if="item.type == 'inputs'">
				<inputs :title="item.title" @input="data[item.name] = $event" :value="data[item.name]" :placeholder="`请输入${item.title}`" :addOn="item.addOn"></inputs>
			</block>
			<block v-else-if="item.type == 'pick'">
				
				<pickers v-if="!item.justWater || isWater" :title="item.title" :value="data[item.name]" @input="data[item.name] = $event" :list="item.list"></pickers>
				
			</block>
			
			<block v-if="item.name == 'name'">
				<view class="fixAuto bb middle " @tap="chooseLocation">
					<view class="w100">
						经纬度
					</view>
					<view class="item">
						<view class="flex">
							<view :class="data.longitude ? 'main' :'gray'">{{data.longitude? data.longitude + ',' + data.latitude:'请到达点位进行定位'}}</view>
							<view class="sIcon-locationFill pl10 gray"></view>
							<input type="text" name="x.latitude|经纬度" :value="data.latitude" style="display: none;"/>
							<input type="text" name="x.longitude" :value="data.longitude" style="display: none;"/>
						</view>
					</view>
				</view>
			</block>
			
			<block v-if="item.name == 'factors'">
				<view class="fixAuto bb middle">
					<view class="w100">
						排放依据
					</view>
					<view @tap="toBasis">
						<view class="flex">
							<view class="cut" style="width: 400rpx;">
								{{data.emissionReference?data.emissionReference:''}}
							</view>
							<view class="sIcon-arrowRight grey"></view>
						</view>
						<input type="text" name="x.emissionReference|排放依据" v-model="data.emissionReference" style="display: none;">
						<input type="text" name="x.factorList" :value="data.factorList" style="display: none;">
					</view>
				</view>
				
				<view class="fixAuto bb middle">
					<view class="w100">
						备案登记
					</view>
					<view @tap="toRecords">
						<view class="flex">
							<view class="cut" style="width: 400rpx;">
								{{data.keepRecordsDate?data.keepRecordsDate:''}}
							</view>
							<view class="sIcon-arrowRight grey"></view>
						</view>
						<input type="text" name="x.keepRecords" v-model="data.keepRecords" style="display: none;">
						<input type="text" name="x.keepRecordsDate|备案登记时间" :value="data.keepRecordsDate" style="display: none;">
					</view>
				</view>
				
			</block>
			
			<input v-if="!item.justWater || isWater" type="text" :name="`x.${item.name}|${item.type=='pick'?'请选择':''}${item.title}`" :value="data[item.name]" style="display: none;">
			
		</block>
		
		
		<view class="plr100 pd30">
			<button form-type="submit" class="btn block rds23">
				确认保存
			</button>
		</view>
		
	</form>
	
	
	
</template>

<script>
	import {Qiniu} from "../../common/upload.js" ;
	export default {
		data() {
			return {
				list:[
					{name : "name" , title : "点位名称" , type : "inputs"},
					{name : "type" , title : "点位类型" , list : ['废水','清下水','废气','VOCS'] , type : "inps"},
					{name : "controlLevel" , title : "级别" , list : ['重点源','非重点源'] , type : "pick"},
					{name : "area" , title : "行政区域" , list : ['衢州市市辖区','衢州市柯城区','衢州市衢江区','衢州市绿色产业集聚区','衢州市龙游县','衢州市江山市','衢州市常山县','衢州市开化县','知慧新城','知造新城'] , type : "inps" },
					{name : "monitorNum" , title : "行业" , list : [] , type : "inputs"},
					{name : "dischargeDirection" , title : "排放去向" , list : ['纳管','直排'] , type : "inps", paddingLeft : 200 },
					{name : "emissionMode" , title : "排放方式" , list : ['连续','间隙'] , type : "inps", paddingLeft : 200 },
					{name : "factors" , title : "主要污染因子" , list : [] , type : "inps" , multi : true },
					{name : "stationAreas" , title : "站房面积" , list : [] , type : "inputs" , addOn : "㎡"},
					{name : "modeType" , title : "门禁" , list : ['正常','故障','无'], type : "pick"},
					{name : "video" , title : "视频" , list : ['正常','故障','无'] , type : "pick"},
					{name : "samplingLocation" , title : "废水混合采样" , list : ['正常','故障','无'] , type : "pick" , justWater : true },
					{name : "airConditioner" , title : "空调" , list : ['正常','故障','无'] , type : "pick"},
					{name : "fireFacilities" , title : "消防设施" , list : ['正常','故障','无'] , type : "pick"},
					{name : "light" , title : "照明" , list : ['正常','故障','无'] , type : "pick"},
					{name : "humidityMeter" , title : "温湿度计" , list : ['正常','故障','无'] , type : "pick"},
					{name : "lightning" , title : "防雷接地" , list : ['正常','故障','无'] , type : "pick"},
					{name : "plumbing" , title : "上下水" , list : ['正常','故障','无'] , type : "pick", justWater : true}
				],
				data : { 
					license : "" , longitude : "" ,companyId : "" , id : "",
					type : "" ,controlLevel:"非重点源" ,
					area : "" ,
					name : "" ,
					monitorNum : "" ,
					dischargeDirection : "" ,
					emissionMode : "" ,
					factors : "" ,
					emissionReference : "" ,
					factorList : "" ,
					keepRecordsDate : "" ,
					keepRecords : "" ,
					stationAreas : "" ,
					modeType : "正常" ,
					samplingLocation : "正常" ,
					airConditioner : "正常" ,
					fireFacilities : "正常" ,
					light : "正常" ,
					humidityMeter : "正常" ,
					lightning : "正常" ,
					plumbing : "正常" ,
					video : "正常" 
				},
				isCompleted : false ,
				qiniu : new Qiniu() ,
				type : 1 ,
				current : 0 ,
				autoCalibrate : 0 ,
				autoCalibrateList : ["非自动校准","自动校准"],
				role : -1
			}
		},
		
		onLoad(param) {
			this.data.companyId = param.id ;
			this.data.id = param.sid ;
			this.loadData();
			this.role = uni.getStorageSync("role");
			
			uni.$on("saveFactors",(e)=>{
				this.$set(this.data , "emissionReference" , e.refers || "" );
				this.$set(this.data , "factorList" , e.factorList || "" );
			});
			
			uni.$on("saveRecords",(e)=>{
				this.$set(this.data , "keepRecords" , e.keepRecords || "" );
				this.$set(this.data , "keepRecordsDate" , e.keepRecordsDate || "" );
			});
		},
		
		computed:{
			
			isWater(){
				return this.data.type && (this.data.type.indexOf("废水") > -1 || this.data.type.indexOf("清下水") > -1 );
			},
			
			isGas(){
				return this.data.type && ( this.data.type.indexOf("废气") > -1 || this.data.type.indexOf("VOCS") > -1 );
			},
			
			factorList(){
				let waters = ["COD", "PH", "总磷", "总氮", "氨氮"];
				let gas = ["二氧化硫", "氮氧化物", "颗粒物", "非甲烷总烃", "氯化氢", "氟化物", "一氧化碳"] ;
				if (!this.data.type) {
					return [ ...waters , ...gas ] ;
				}
				if (this.isWater) {
					return waters ;
				}
				if (this.isGas) {
					return gas ;
				}
				return [ ...waters , ...gas ] ;
			},
			
			_factors(){
				if (!!this.data.factorList) {
					return this.data.factorList ;
				}
				if (this.data.factors) {
					return JSON.stringify(this.data.factors.split("、").map(c=>({
						factor : c 
					})));
				}
				return '' ;
			}
		},
		
		methods: {
			
			submit(e){
				console.log("e: ",e);
				this.form.submit(e,this.success);
			},
			
			toBasis(){
				uni.navigateTo({
					url:`/pages/emission/emission?factorList=${this._factors}&type=${this.data.type?this.data.type:''}&emissionReference=${this.data.emissionReference?this.data.emissionReference:''}`,
					fail: (e) => {
						console.log("e: ",e);
					}
				})
			},
			
			toRecords(){
				uni.navigateTo({
					url:`/pages/records/records?keepRecordsDate=${this.data.keepRecordsDate?this.data.keepRecordsDate:''}&keepRecords=${this.data.keepRecords?this.data.keepRecords:''}`,
					fail: (e) => {
						console.log("e: ",e);
					}
				})
			},
			
			loadData:function(e){
				if(!this.data.id){
					return ;
				}
				if (this.role == 2) {
					uni.setNavigationBarTitle({
						title:"修改废水点位信息"
					});
				}
				var data = {id:this.data.id} ;
				this.kit.get("/api/station/info",data,res => {
					this.data = res.data ;
				});
			},
			
			toUploadImage:function(key){
				var sourceTypes = ["album", "camera"];
				uni.showActionSheet({
					itemList:["从相册选择","从相机拍照"],
					success : res => {
						uni.chooseImage({
							sourceType:[sourceTypes[res.tapIndex]],
							count:1,
							success: res =>{
								uni.showNavigationBarLoading();
								this.qiniu.upload(res.tempFilePaths[0] , res =>{
									this.$set(this.data,key,res);
									uni.hideNavigationBarLoading();
								});
							}
						});
					}
				});
			},
			
			chooseLocation:function(e){
				uni.getLocation({
					type:"gcj02",
				    success:  (res) =>{
						console.log(res);
						this.data.latitude = parseFloat(res.latitude).toFixed(5) ;
						this.data.longitude =  parseFloat(res.longitude).toFixed(5);
				    },
					fail : (e)=>{
						console.log(e);
						if (e.errMsg == 'getLocation:fail auth deny') {
							uni.showModal({
								title:'提示',
								content:'获取位置未授权，请先授权获取当前地理位置',
								showCancel:true,
								confirmColor:'#3385ff',
								success : (e)=>{
									if (e.confirm) {
										uni.openSetting({
										  success :(res) => {
											   console.log(res)
											  if (res.authSetting['scope.userLocation']) {
											  	 this.chooseLocation();
											  }
										  }
										});
									}
								}
							});
						}else if(e.errMsg == 'getLocation:fail'){
							uni.showToast({
								title: '定位失败',
								icon : 'none'
							});
						}
					},
				});
			},
			
			success:function(e){
				this.kit.goBack();
				setTimeout(e => {
					uni.showToast({
						title: this.data.id ? '修改成功' : '添加成功',
						icon : 'success'
					});
				},200);
			}
		}
	}
</script>

<style>
page{
	background-color: #fff;
}
.fixAuto{
	padding: 30upx 10upx;
}
</style>
